#include "stdafx.h"
#include "charset/textencodemanager.hpp"

namespace JHCPP
{
	namespace charset
	{	
		CTextEncodeManager::CTextEncodeManager()
		{

		}

		CTextEncodeManager::~CTextEncodeManager()
		{
		}

		void CTextEncodeManager::add(CTextEncode::Ptr pEncode)
		{
			add(pEncode, pEncode->canonicalName());
		}

		void CTextEncodeManager::add(CTextEncode::Ptr pEncode, const std::string& name)
		{
			CRWLock::ScopedLock lock(m_lock, true);
			m_encodeMap[name] = pEncode;
		}

		void CTextEncodeManager::remove(const std::string& name)
		{
			CRWLock::ScopedLock lock(m_lock, true);
			m_encodeMap.erase(name);
		}

		CTextEncode::Ptr CTextEncodeManager::find(const std::string& name) const
		{
			CRWLock::ScopedLock lock(m_lock);

			EncodeMap::const_iterator it = m_encodeMap.find(name);
			if (it != m_encodeMap.end())
				return it->second;

			for (it = m_encodeMap.begin(); it != m_encodeMap.end(); ++it)
			{
				if (it->second->isA(name))
					return it->second;
			}
			return CTextEncode::Ptr();
		}

	}//end of namespace charset
}//end of namespace JHCPP
